﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace SpaceShooter
{
    public enum KeyPressType
    {
        Single,
        Continuous
    }

    public class KeyBehaviour
    {
        public KeyBehaviour()
        {
            KeyboardKey = Keys.None;
            GamepadButton = Buttons.Start;
            IsActive = false;
            KeyPressType = KeyPressType.Single;
        }

        public Keys KeyboardKey { get; set; }
        public Buttons GamepadButton { get; set; }
        public KeyPressType KeyPressType { get; set; }
        public bool IsActive { get; set; }
    }

    public class ControlState
    {
        public KeyboardState KeyboardState { get; set; }
        public GamePadState GamePadState { get; set; }
    }

    public class KeyStrokeTable
    {
        public KeyBehaviour FlyLeft { get; set; }
        public KeyBehaviour FlyRight { get; set; }
        public KeyBehaviour FlyForward { get; set; }
        public KeyBehaviour FlyBack { get; set; }
        public KeyBehaviour ChangeWeapon { get; set; }
        public KeyBehaviour ShootWeapon { get; set; }
        public KeyBehaviour ShieldsUp { get; set; }
    }

    public static class GameControlHelper
    {
        public static ControlState GetKeyPressedState(ControlState lastControlState, KeyStrokeTable keyStrokeTable)
        {
            var currentKeyState = GetCurrentKeyState();

            if (lastControlState == null)
            {
                lastControlState = currentKeyState;
            }

            GetActiveState(keyStrokeTable.ChangeWeapon, lastControlState, currentKeyState);
            GetActiveState(keyStrokeTable.FlyBack, lastControlState, currentKeyState);
            GetActiveState(keyStrokeTable.FlyForward, lastControlState, currentKeyState);
            GetActiveState(keyStrokeTable.FlyLeft, lastControlState, currentKeyState);
            GetActiveState(keyStrokeTable.FlyRight, lastControlState, currentKeyState);
            GetActiveState(keyStrokeTable.ShieldsUp, lastControlState, currentKeyState);
            GetActiveState(keyStrokeTable.ShootWeapon, lastControlState, currentKeyState);

            return currentKeyState;
        }

        public static void GetActiveState(KeyBehaviour key, ControlState lastControlState, ControlState currentControlState)
        {
            key.IsActive = (key.KeyPressType == KeyPressType.Single ? (currentControlState.KeyboardState.IsKeyDown(key.KeyboardKey) && !lastControlState.KeyboardState.IsKeyDown(key.KeyboardKey)) : (currentControlState.KeyboardState.IsKeyDown(key.KeyboardKey))) ||
                (key.KeyPressType == KeyPressType.Single ? (currentControlState.GamePadState.IsButtonDown(key.GamepadButton) && !lastControlState.GamePadState.IsButtonDown(key.GamepadButton)) : (currentControlState.GamePadState.IsButtonDown(key.GamepadButton)));
        }

        public static ControlState GetCurrentKeyState()
        {
            return new ControlState()
            {
                GamePadState = GamePad.GetState(PlayerIndex.One),
                KeyboardState = Keyboard.GetState()
            };    
        }
    }
}
